package com.gxa.service.impl;
import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.common.ResponseData;
import com.gxa.common.jwt.JwtUtils;
import com.gxa.mapper.AddressBookMapper;
import com.gxa.pojo.Employee;
import com.gxa.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class AddressBookServiceImpl implements AddressBookService{

    @Autowired(required =false)
    private AddressBookMapper addressBookMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult selectEmp(Integer currentPage ,Integer pageSize ) {
        Integer param1 =(currentPage-1)*pageSize;
        Integer param2 =currentPage*pageSize;
        List<Employee> employeeList=addressBookMapper.selectEmp(param1,param2);
        Integer count=addressBookMapper.count();
        //将list和count条数塞入r,响应成功返回
        JsonResult r = new JsonResult();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(employeeList);
        r.setCount(count);
        return r;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult searchEmployee(PageRecv<Employee> pageRecv) {
        //对查询页面参数计算
        Integer currentPage=pageRecv.getCurrentPage();
        Integer pageSize=pageRecv.getPageSize();
        Integer limit1=(currentPage-1)*pageSize;
        pageRecv.setCurrentPage(limit1);
        //获取查询条件实体
        Employee employee=pageRecv.getData();
        //调用mapper方法，返回查询结果list
        List<Employee> list=addressBookMapper.findEmployeeMapper(pageRecv);
        Integer count =addressBookMapper.findEmployeeCountMapper(pageRecv);
        //将list和count条数塞入r,响应成功返回
        JsonResult r=new JsonResult();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setCount(count);
        r.setData(list);
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData login(Employee employee) {
        ResponseData RD = new ResponseData();
        Employee emp = addressBookMapper.login(employee);
        System.out.println("==============");
        System.out.println(emp);
        if(emp!=null){
            RD.setCode(200);
            RD.setMsg("登录成功");
            RD.setData(emp);
            String uuid = UUID.randomUUID().toString().replace("-","");
            String token = JwtUtils.createJwt(uuid,"GXA",employee.getEmployee_name(),JwtUtils.TTMILLIS);
            RD.setToken(token);
        }else{
            RD.setCode(201);
            RD.setMsg("登录失败");
        }
        return RD;
    }



//    @Override
//    public JsonResult searchEmployee(Integer currentPage, Integer pageSize) {
//        Integer param1 =(currentPage-1)*pageSize;
//        Integer param2 =currentPage*pageSize;
//        List<Employee> employeeList=addressBookMapper.searchEmployee(param1,param2);
//        Integer count=addressBookMapper.count();
//        JsonResult r = new JsonResult();
//        r.setCode(200);
//        r.setMsg("查询成功");
//        r.setCount(count);
//        r.setData(employeeList);
//        return r;
//    }

}
